// subpage/pages/order/order.js
import service from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList:[],
    isShowFirst: 0,
    isRefresh: false,
    totalPage: 0,
    page: 1,
    isShowBottom: false,
    hasNext:false,
    isRequest:true,
    ordertype:false,
    islogin:true,
    showCancel: false,
    sinId: '',
    more: false,
    info: {},
  },
  handleMore(e){
    console.log('e',e)
    this.setData({
      more: !this.data.more,
      info: e.currentTarget.dataset.id
    })
  },
  preventTouchMove(){
    return false
  },
  showCancels(){
    this.setData({
      showCancel: false
    })
  },
  //复制订单号
  copyOrder(e){
    wx.setClipboardData({
      data:e.currentTarget.dataset.url,
      success:function (res) {
        wx.showToast({
          title: '复制成功',
          icon: 'none'
        })
      }
    })
  },
  btnCan(e){
    this.setData({
      showCancel: true,
      sinId: e.currentTarget.dataset.id
    })
  },
  confirmCancel(){
    let id = this.data.sinId
    let this_ = this
    service("POST",`/invoicing/cancel/${id}`).then(res => {
      this_.setData({showCancel: false, sinId: ''})
      if(res.data.code ==0){
        wx.showToast({
          title: '已取消开票申请',
        })
        this_.getOrderList(1)
      }else{
        wx.showToast({
          title: '取消失败',
          icon: 'error'
        })
      }
    })
  },

  onConfirm(event) {
    const [start, end] = event.detail;
    this.setData({
      show: false,
      date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  goLogin(){
    wx.redirectTo({
      url: '/subpage/pages/login/login',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let order = this.selectComponent("#orderlist")
    this.setData({
      orderlist: order
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(wx.getStorageSync('token')){
      this.setData({
        islogin: true,
        isRequest: true,
        dataList: [],
        page:1
      })
      this.getOrderList()
    }else{
      this.setData({
        islogin: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  //自定义上拉加载
  loadMore(type) {
      //如果总页数 小于等于 当前页数 提示无更多数据
      if(!this.data.isRefresh){
        console.log('加载中')
        if (this.data.hasNext) {
          this.setData({
            page: this.data.page + 1,
            isRefresh:true
          })
          this.selectComponent("#orderlist").noMore('loading')
          this.getOrderList(2)
        } else{
          console.log('没有更多')
          this.selectComponent("#orderlist").noMore('nomore')
          console.log('没有更多')
          // this.setData({
          //   isShowBottom: true
          // })
        }
      }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("下拉刷新")
    if (!this.data.isRefresh) {
      //如果总页数 小于等于 当前页数 提示无更多数据
      if (this.data.hasNext) {
        this.setData({
          page: this.data.page + 1,
          isRefresh:true
        })
        this.getOrderList(2)
      } else{
        console.log('没有更多1')
        this.selectComponent("#orderlist").noMore()
        this.setData({
          isShowBottom: true
        })
      }
    }
  },
  /* 切换显示内容 */
  changeShowType(type){
    this.data.isShowFirst = type;
  },
  /* 手动滑动swiper触发的事件 */
  // onChange(e){
  //   console.log(e)
  //   this.setData({
  //     isShowFirst: e.detail.current+1
  //   })
  // },
//自定义下拉刷新
refresh(e){
  console.log('父组件刷新',e)
  this.setData({
    page: 1,
    
  })
  this.getOrderList(1)
},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changeTab(e) {
    if(this.data.isRequest){
      this.setData({
        isShowFirst: e.target.dataset.id,
        dataList: [],
         page:1
      })
      this.getOrderList()
    }
  },

  getOrderList(type) {
    console.log('刷新形式',type)
    let that = this
    if(that.data.isRequest === false){
      return
    }
    this.setData({
      isRequest: false
    })
    let sear
    if(this.data.isShowFirst == 0){
      sear = {page: this.data.page, type: 0}
    }else{
      sear = {page: this.data.page, state: this.data.isShowFirst, type: 0}
    }
     
    service("POST","/invoicing/list",sear).then(res => {
      console.log('列表', res)
      if(res.data.code == 414){
        this.setData({
          islogin: false
        })
        wx.setStorageSync('token', '')
        return
      }
      that.setData({
        isRefresh: false
      })
      if(type==1){
        that.setData({
          dataList: []
        })
      }
      if(res.data.code ==0){
        let data = res.data.history
        that.setData({
          dataList: [...that.data.dataList,...res.data.page.list],
          isRefresh: false,
          hasNext:res.data.page.hasNext,
          isRequest:true
        })
      }
      if(type == 1) {
        that.selectComponent("#orderlist").reSet()
      }
      
    })
  },
  //跳转
  apply(){
    wx.redirectTo({
      url: '/user/pages/invoce/invoce',
    })
  },
  head(){
    wx.redirectTo({
      url: '/user/pages/invoce/head',
    })
  },

  //复制
  copy: function (e) {
    console.log('复制',e)
    wx.setClipboardData({
      data: e.target.dataset.oid,
    });
    // wx.showToast({
    //   title: '复制成功',
    // })
  }
})